const app = getApp()
const api = app.globalData.api
Page({
    data: {
        num: 0,
        maxNum: 0,
        key: '',
        item: {},
        type: '非条码',
        warehouseOption: [],
        warehousePopup: false,
        positionOption: [],
        positionPopup: false,
    },
    // 获取仓库
    getWarehouse: async function () {
      let res = await api.curlGet('?cla=inStock&fun=getWarehouse')
      if (res.code === 1) {
          this.setData({
            warehouseOption: res.data
          })
      }
    },
    // 获取库位
    getPosition: async function (warehouseId, positionId) {
      if(warehouseId){
        let that = this
        let res = await api.curlPost('?cla=inStock&fun=getPosition', {warehouseId: warehouseId, positionId: positionId})
        if (res.code === 1) {
          that.setData({
            positionOption: res.data.positionOption,
            ['item.position']: res.data.position,
            positionId: res.data.positionId,
          })
        }
      }
    },
    main: async function () {
        await this.getWarehouse()
        await this.getPosition(this.data.item.warehouseId, this.data.item.positionId)
    },
    putIn: async function (e) {
        let data = e.detail.value
        if (data) {
          if(parseFloat(this.data.num) <= 0){
            api.showToast('入库数量必须大于0')
            return
          }
          // 初始化部分值
          //data['rate'] = api.initNaN(data, 'rate')
          data['inNum'] = this.data.num
          data['type'] = '本次入库'
          data['key'] = this.data.key
          data['time'] = api.formatDate(new Date().getTime(), 'datetime')
          let mergedObj = { ...this.data.item, ...data };  
          api.showToast('确认成功')
          wx.setStorageSync('inData', mergedObj)
          wx.navigateBack({
              delta: 1
          })
        }
    },
    onStepperChange(e) {  
      this.setData({  
        num: e.detail,
      });  
    },  
    inputValue(e) {  
      let obj = e.currentTarget.dataset
      let field = 'item.'+obj.field
      this.setData({  
        [field]: e.detail,
      });  
    },
    // 关闭
    onClose: function (e) {
      let obj = e.currentTarget.dataset
      if (obj) {
          this.setData({[obj.target]: false})
      }
    },
    funRouting(type, detail) {
      switch (type) {
          case 'orderDate':
              return api.formatDate(detail)
          case 'warehouseId':
              let warehouse = detail.value
              this.setData({
                  ['item.warehouse']: warehouse['text']
              })
              return warehouse['value']
          case 'positionId':
              let position = detail.value
              this.setData({
                  ['item.position']: position['text']
              })
              return position['value']
      }
  },
  // 确认选择
  onConfirm: async function (e) {
      let obj = e.currentTarget.dataset
      let prefix = obj.prefix ? obj.prefix : 'item'
      let field = (prefix ? prefix + '.' : '') + obj.field
      let val = this.funRouting(obj.field, e.detail)
      this.setData({
          [obj.target]: false,
          [field]: val,
      })
      // 获取库位
      if(obj.field === 'warehouseId'){
        await this.getPosition(val, this.data.item.positionId)
      }
  },
  showPopup: function (obj) {
    let popup = obj.currentTarget.dataset.show
    if (popup) {
        this.setData({
            [popup]: true
        });
    }
  },
    onLoad: async function (options) {
        wx.removeStorageSync('inData')
        let string = options.data ? options.data : '',
            key = options.key ? options.key : ''
        wx.setNavigationBarTitle({
            title: '按物料入库'
        })
        if (string.length < 1) {
            api.showToast('参数错误')
        } else {
            const data = api.jsonToArray(api.urlDecode(string))
            let inNum = data.inNum ? data.inNum : 0
            this.setData({
                key: key,
                num: inNum > 0 ? inNum : data.orderNum - data.num,
                maxNum: data.inStockLimit != '1' ? (data.orderNum - data.num + inNum) : Number.MAX_SAFE_INTEGER,
                item: data
            })
            console.log(this.data)
        }
        await this.main()
    }
});